/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.belfalas.simy86;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.Timer;
import org.belfalas.simy86.gui.Window2;
import org.belfalas.simy86.gui.window;

/**
 *
 * @author belfalas
 */
public class Simulador {

    private static Y86 y86;
    private static Window2 w;
    private static window w2;

    public static void main(String args[]) throws IOException {

        y86 = new Y86();

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String linha;

        System.out.println("*** Y86 Simulator ***");

        System.out.print("y86> ");
        while ((linha = br.readLine()) != null) {
            char c = linha.charAt(0);

            switch (c) {
                case 'r': {
                    y86.setPC(0);
                    break;
                }
                case 'd': {
                    try {
                        Y86.memoria.dumpMemory(Integer.parseInt(linha.substring(2), 16));
                    } catch (Exception ex) {
                        Y86.memoria.dumpMemory(0);
                    }
                    break;
                }
                case 'e': {
                    System.exit(0);
                }
                case 'l': {
                    String filename;
                    try {
                        filename = linha.substring(2);
                        loadFile(filename, y86);
                    } catch (Exception ex) {
                        System.out.println(ex.getMessage());
                        System.out.println("Usage:l filepath");
                    }
                    break;
                }
                case 'j': {
                    try {
                        y86.setPC(Integer.parseInt(linha.substring(2), 16));
                    } catch (Exception ex) {
                        System.out.println("jump failed, check your number");
                    }
                    break;
                }
                case 's': {
                    y86.setPC(y86.getPC() + 1);
                    break;
                }
                case 'x': {
                    try {
                        y86.cycle(Integer.parseInt(linha.substring(2)));
                    } catch (Exception ex) {
                        y86.cycle();
                    }
                    break;
                }
                case 'p': {
                    for (Registradores reg : Registradores.values()) {
                        System.out.println(reg.toString() + "=" + reg.reg);
                    }
                    break;
                }
                case 'g': {
                    w = new Window2();
                    w.start();
                    y86.issueDelay(30);
                    if (Window2.timer == null) {
                        Window2.timer = new Timer(20, new ActionListener() {

                            @Override
                            public void actionPerformed(ActionEvent e) {
                                //y86.cycle();
                                w.redraw();
                                //y86.setPC(y86.getPC()+1);
                            }
                        });
                    }

                    Window2.timer.setRepeats(true);
                    Window2.timer.start();
                    break;
                }
                case 'h': {
                    System.out.println(
                            "r            : reset\n"
                            + "d [address]  : dump memory region\n"
                            + "e            : exit\n"
                            + "l filename   : load assembly file\n"
                            + "j address    : jump to address\n"
                            + "s            : step\n"
                            + "x [instrs]   : execute <instrs> instructions or until halt\n"
                            + "p            : print registers\n"
                            + "g            : attach screen\n");
                    break;
                }
            }
            System.out.print("y86> ");
        }

    }

    private static void loadFile(String fileName, Y86 y86) {
        File file = new File(fileName);
        ReadFile.parse(file, y86);
    }
}
